﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Mvc.HelpDesk.Data;

namespace Mvc.HelpDesk.Controllers
{ 
    public class SubCategoriaController : Controller
    {
        private HelpDeskEntities db = new HelpDeskEntities();

        //
        // GET: /SubCategoria/

        public ViewResult Index()
        {
            var subcategorias = db.SUBCATEGORIAs.Include("CATEGORIA");
            return View(subcategorias.ToList());
        }

        //
        // GET: /SubCategoria/Details/5

        public ViewResult Details(int id)
        {
            SUBCATEGORIA subcategoria = db.SUBCATEGORIAs.Single(s => s.IDSUBCATEGORIA == id);
            return View(subcategoria);
        }

        //
        // GET: /SubCategoria/Create

        public ActionResult Create()
        {
            ViewBag.IDCATEGORIA = new SelectList(db.CATEGORIAs, "IDCATEGORIA", "NOMECATEGORIA");
            return View();
        } 

        //
        // POST: /SubCategoria/Create

        [HttpPost]
        public ActionResult Create(SUBCATEGORIA subcategoria)
        {
            if (ModelState.IsValid)
            {
                db.SUBCATEGORIAs.AddObject(subcategoria);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.IDCATEGORIA = new SelectList(db.CATEGORIAs, "IDCATEGORIA", "NOMECATEGORIA", subcategoria.IDCATEGORIA);
            return View(subcategoria);
        }
        
        //
        // GET: /SubCategoria/Edit/5
 
        public ActionResult Edit(int id)
        {
            SUBCATEGORIA subcategoria = db.SUBCATEGORIAs.Single(s => s.IDSUBCATEGORIA == id);
            ViewBag.IDCATEGORIA = new SelectList(db.CATEGORIAs, "IDCATEGORIA", "NOMECATEGORIA", subcategoria.IDCATEGORIA);
            return View(subcategoria);
        }

        //
        // POST: /SubCategoria/Edit/5

        [HttpPost]
        public ActionResult Edit(SUBCATEGORIA subcategoria)
        {
            if (ModelState.IsValid)
            {
                db.SUBCATEGORIAs.Attach(subcategoria);
                db.ObjectStateManager.ChangeObjectState(subcategoria, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.IDCATEGORIA = new SelectList(db.CATEGORIAs, "IDCATEGORIA", "NOMECATEGORIA", subcategoria.IDCATEGORIA);
            return View(subcategoria);
        }

        //
        // GET: /SubCategoria/Delete/5
 
        public ActionResult Delete(int id)
        {
            SUBCATEGORIA subcategoria = db.SUBCATEGORIAs.Single(s => s.IDSUBCATEGORIA == id);
            return View(subcategoria);
        }

        //
        // POST: /SubCategoria/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            SUBCATEGORIA subcategoria = db.SUBCATEGORIAs.Single(s => s.IDSUBCATEGORIA == id);
            db.SUBCATEGORIAs.DeleteObject(subcategoria);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}